Skip to main content
ℬ㏒.㎈ℓℯℛ.ⓧⓨℤ

Slack Bingo bot

Companies love Slack, and lots of services integrate via their API. While slash commands are simple web hooks, bots can listen in to the conversations taking place on a channel and participate. Meet @bingo! Add your buzzword to the list, and whoever mentions the word in the channel first will get the bingo. It's a really simple unintelligent bot.

@bingo hello
@bingo hello

To add words to the list, private message @bingo e.g. 'add banana reload "internet of things"'. The user who adds the word is excluded from getting the bingo.

It's stopped working! How do I **reload** the **internet of things**?
It's stopped working! How do I reload the internet of things?

The first time a keyword is used, @bingo will shout Bingo and show a GIF!

Can I put a **selfie** in my **spreadsheet**?
Can I put a selfie in my spreadsheet?

Installation #

The source for bingo is available at :octocat:bcaller/slack-bingo-bot.

:fa-slack:Slack Team Settings #

Custom integrations > Bots > ... > Get a token.

Install #

Clone, install dependencies, set the SLACK_TOKEN environment variable and run npm start

git clone https://github.com/bcaller/slack-bingo-bot.git
cd slack-bingo-bot`
npm install
SLACK_TOKEN=xtokenX npm start

A server is not really necessary: a WebSocket connection to Slack can be created from any computer. When not running, @bingo just will appear offline.

You can change the starting words in initial.txt

In the channel #

/invite @bingo invites the bingo bot.

Code: Make a :fa-slack:bot #

We used the awesome botkit library. GIFs come from the giphy API. Uses arrow functions, let and template strings, so use an appropriate Node version.

It's important to note that there are many events and API calls which are not explicitly documented in the botkit README. However, the Slack API calls are available as bot.api.x.y(data, cb): see my use of reactions.add. If you increase the logLevel, you can see which events are being triggered.

We start the WebSocket connection (Real Time Messaging) with

var controller = require('botkit').slackbot()
controller.spawn({
    token: process.env.SLACK_TOKEN
}).startRTM()

We spy on all messages with

controller.on('ambient', (bot, message) => {
    /*process message*/
})

We can reply to message with bot.reply(message, {/*reply data*/})

Listen for bingo's secret direct message "cheat":

controller.hears('cheat', ['direct_message'], (bot, message) => {
    react(bot, 'wink', message, noop)
    bot.reply(message, words.map(w => w.word).join(', '))
})

Things get more complicated when you need to have actual conversations with your bots and deal with multiple teams, but this is a quick way to get started.